package com.lyeducation.controllers.chapter;

import com.lyeducation.controllers.dto.ChapterDTO;
import com.lyeducation.core.course.ChapterService;
import com.lyeducation.repository.ChapterRepository;
import com.lyeducation.util.StringUtils;
import com.lyeducation.util.controllerUtil.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;


/**
 * @ClassName ChapterController @Description @Author Sky1in3 @Date 2019/1/18 0:38 @Version 1.0
 */
@Api(
        value = "章节",
        tags = {"章节模块接口，{mId}菜单id"},
        description = "章节模块接口"
)
@RestController
@RequestMapping("/chapter/{mid}")
@Transactional(rollbackFor = Exception.class, readOnly = false, propagation =
        Propagation.REQUIRED)
public class ChapterController {
    @Autowired
    private ChapterRepository chapterRepository;
    @Autowired
    private ChapterService chapterService;

    @ApiOperation(value = "新增章节", notes = "新增章节")
    @PostMapping("/newChapter")
    public JsonResult newChapter(@RequestBody ChapterDTO chapterDTO) {
        return JsonResult.putSuccess(chapterService.addChapter(chapterDTO));
    }

    @ApiOperation(value = "修改章节", notes = "修改章节")
    @PostMapping("/updateChapter")
    public JsonResult updateChapter(@RequestBody ChapterDTO chapterDTO) {
        return JsonResult.putSuccess(chapterService.updateChapter(chapterDTO));
    }

    @ApiOperation(value = "软删除章节中的视频", notes = "软删除章节中的视频")
    @GetMapping("/delChapter/{chapterId}")
    public JsonResult delChapter(@PathVariable String chapterId) {
        return chapterService.delChapter(chapterId);
    }

    @ApiOperation(value = "删除章节", notes = "删除章节")
    @GetMapping("/deleteChapter/{chapterId}")
    public JsonResult deleteChapter(@PathVariable String chapterId) {
        return JsonResult.putSuccess(chapterService.deleteChapter(chapterId));
    }

    @ApiOperation(value = "章节详情", notes = "章节详情")
    @GetMapping("/chapterInfo/{chapterId}")
    public JsonResult chapterInfo(@PathVariable String chapterId) {
        if (StringUtils.isNotBlank(chapterId)) {
            return JsonResult.putSuccess(chapterService.chapterInfo(chapterId));
        } else {
            return JsonResult.putFail("章节id不能为空！");
        }
    }

    @ApiOperation(value = "首页章节详情", notes = "首页章节详情")
    @GetMapping("/chapterInfoIndex")
    public JsonResult chapterInfoIndex(@RequestParam("openid") String openid, @RequestParam("chapterId") String chapterId) {
        if (StringUtils.isNotBlank(chapterId)) {
            return JsonResult.putSuccess(chapterService.chapterInfoIndex(openid, chapterId));
        } else {
            return JsonResult.putFail("章节id不能为空！");
        }
    }

    @ApiOperation(value = "章节列表", notes = "章节列表")
    @GetMapping("/chapterList")
    public JsonResult chapterList(@RequestParam("courseId") String courseId) {
        return JsonResult.putSuccess(chapterService.chapterEntityList(courseId));
    }
}
